package com.junlong0716.enjoylearning.ui

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import com.blankj.utilcode.util.ToastUtils
import com.junlong0716.enjoylearning.MyApp
import com.junlong0716.enjoylearning.R
import com.junlong0716.enjoylearning.constant.NetConstant
import com.junlong0716.enjoylearning.entity.rxevent.RefreshNote
import com.junlong0716.enjoylearning.net.api.NetApi
import com.junlong0716.module.common.rxbus.RxBus
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_add_note.*
import org.json.JSONObject

class AddNoteActivity : AppCompatActivity() {

    private var isUpdateNote = false
    private var noteId = 0
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_add_note)

        isUpdateNote = intent.getBooleanExtra("IS_UPDATE_NOTE", false)


        val selectText = intent.getStringExtra("SELECT_TEXT")
        val webId = intent.getIntExtra("WEB_ID", 0)
        tv_knowledge_content.text = selectText
        rl_back.setOnClickListener { finish() }
        tv_title.text = "记笔记"
        rl_save.setOnClickListener {
            val noteContent = det_note_content.text.toString().trim()
            if (noteContent.isEmpty()) {
                ToastUtils.showShort("记录点什么吧！")
                return@setOnClickListener
            }

            if (isUpdateNote) {
                requestUpdateNote(noteId,noteContent)
            } else
                requestAddNote(selectText, webId, noteContent)
        }

        if (isUpdateNote) {
            val noteContent = intent.getStringExtra("NOTES_CONTENT")
            noteId = intent.getIntExtra("NOTES_ID", 0)

            det_note_content.setText(noteContent)
        }
    }

    private fun requestUpdateNote(noteId: Int, noteContent: String) {
        val map = HashMap<String, Any>()
        map["id"] = noteId
        map["noteContent"] = noteContent


        OkGo.post<String>(NetConstant.REQUEST_HEADER + NetApi.API_EDIT_NOTE)
                .tag(this)
                .upJson(JSONObject(map))
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        val jsonObj = JSONObject(response!!.body())
                        val code = jsonObj.getString("code")
                        if (code == "200") {
                            ToastUtils.showShort("保存成功！")
                            RxBus.getDefault().post(RefreshNote())
                            finish()
                        }
                    }

                    override fun onError(response: Response<String>?) {
                        ToastUtils.showShort("网络出错！请检查网络！")
                    }
                })
    }

    private fun requestAddNote(selectText: String, webId: Int, noteContent: String) {
        val map = HashMap<String, Any>()
        map["userId"] = MyApp.instance().userAccount!!.id
        map["knowledgeId"] = webId
        map["remarks"] = selectText
        map["noteContent"] = noteContent

        OkGo.post<String>(NetConstant.REQUEST_HEADER + NetApi.API_ADD_NOTE_CONTENT)
                .tag(this)
                .upJson(JSONObject(map))
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {
                        val jsonObj = JSONObject(response!!.body())
                        val code = jsonObj.getString("code")
                        if (code == "200") {
                            ToastUtils.showShort("保存成功！")
                            RxBus.getDefault().post(RefreshNote())
                            finish()
                        }
                    }

                    override fun onError(response: Response<String>?) {
                        ToastUtils.showShort("网络出错！请检查网络！")
                    }
                })
    }


    override fun finish() {
        super.finish()
        if (!isUpdateNote) {
            overridePendingTransition(R.anim.anim_up, R.anim.anim_down)
        }
    }
}
